
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.cart.controller;

import com.lxk.oms.cart.domain.bo.OmsCartBo;
import com.lxk.oms.cart.domain.vo.OmsCartVo;
import com.lxk.oms.cart.service.IOmsCartService;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户购物车
 *
 * @author LXK-RY5
 * @date 2024-08-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/cart")
public class OmsCartController extends BaseController {

    private final IOmsCartService omsCartService;

    /**
     * 查询用户购物车列表
     */
    @GetMapping("/list")
    public TableDataInfo<OmsCartVo> list(OmsCartBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return omsCartService.queryPageList(bo, pageQuery);
    }

    /**
     * 新增用户购物车
     */
    @Log(title = "用户购物车", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OmsCartBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        return toAjax(omsCartService.insertByBo(bo));
    }

    /**
     * 修改用户购物车
     */
    @Log(title = "用户购物车", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OmsCartBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        return toAjax(omsCartService.updateByBo(bo));
    }

    /**
     * 删除用户购物车
     *
     * @param ids 主键串
     */
    @Log(title = "用户购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(omsCartService.deleteWithValidByIds(List.of(ids), true));
    }

}
